package com.tanhua.autoconfig.templates;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.baidu.aip.face.AipFace;
import com.baidu.aip.util.Base64Util;
import com.tanhua.autoconfig.properties.AipFaceProperties;
import com.tanhua.autoconfig.properties.OssProperties;
import org.json.JSONObject;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

public class AipFaceTemplate {

    private AipFaceProperties aipFaceProperties;

    public AipFaceTemplate(AipFaceProperties aipFaceProperties) {
        this.aipFaceProperties = aipFaceProperties;
    }

public boolean detect(byte[]bytes ){
    //1.将图片转换为byte数组
    //2.将byte数组转换为base64的字符串 需要将这个图片转换成一个String字符串进行检测
    String image = Base64Util.encode(bytes);


    // 初始化一个AipFace
    AipFace client = new AipFace(aipFaceProperties.getAppId(), aipFaceProperties.getApiKey(), aipFaceProperties.getSecretKey());
    // 传入可选参数调用接口
    HashMap<String, String> options = new HashMap<String, String>();
    options.put("face_field", "age");
    options.put("max_face_num", "2");
    options.put("face_type", "LIVE");
    options.put("liveness_control", "LOW");


    String imageType = "BASE64";

    // 人脸检测
    JSONObject res = client.detect(image, imageType, options);
    System.out.println(res.toString(2));
    System.out.println(res.get("error_code"));
    int code = res.getInt("error_code");
    return code==0;

}




}
